package foodhelper.mtech.com;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import foodhelper.about.About;
import foodhelper.database.Database;
import foodhelper.healthtip.HealthTip;
import foodhelper.menu.HealthyMenu;
import foodhelper.service.FoodHelperServiceFactory;
import foodhelper.service.IFoodHelperService;

public class MainActivity extends Activity {

	
	private ImageButton m_ibmBtn;
	private ImageButton m_menuBtn;
	private ImageButton m_dietBtn;
	private ImageButton m_aboutBtn;
	private ImageButton m_quitBtn;
	private ImageButton m_healtipBtn;
	private Database mDb; 
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mDb = Database.getInstance(this);
		if (getFlag()==false){
			loadData();
			IFoodHelperService service = FoodHelperServiceFactory.getInstance().getService();
			String SDcardPath = Environment.getExternalStorageDirectory().getPath();
			String myDbPath = SDcardPath + "/" + "flagDatabaseFoodhelper";
			try {
				service.WriteData(myDbPath, "true");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		initControls();
	}
	public void loadData(){
		m_ibmBtn = (ImageButton) findViewById(R.id.bmiBtn);
//		m_ibmBtn.setBackgroundResource(R.drawable.buttondrawable);
		m_menuBtn = (ImageButton) findViewById(R.id.menuBtn);
		m_dietBtn = (ImageButton) findViewById(R.id.dietBtn);
		m_aboutBtn = (ImageButton) findViewById(R.id.aboutBtn);
		m_quitBtn = (ImageButton) findViewById(R.id.quitBtn);
		m_healtipBtn = (ImageButton) findViewById(R.id.healthtipBtn);
		
		

	}
	
	public void initControls(){
		overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
		m_ibmBtn.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN)
                	m_ibmBtn.setImageResource(R.drawable.calculator);
                try{
                	Thread.sleep(1000);
                }catch (Exception ex){
                	
                }
                if(event.getAction()==MotionEvent.ACTION_UP)
                	m_ibmBtn.setImageResource(R.drawable.calculator2);
				startActivity(new Intent(MainActivity.this, BmiCaculator.class)); 
                return false;
            }
			
        });
		
		
		m_menuBtn.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN)
                	m_menuBtn.setImageResource(R.drawable.list);
                if(event.getAction()==MotionEvent.ACTION_UP)
                	m_ibmBtn.setImageResource(R.drawable.list2);
                startActivity(new Intent(getApplicationContext(), HealthyMenu.class)); 
				//overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
                return false;
            }
			
        });
		
		
		m_aboutBtn.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN)
                	m_aboutBtn.setImageResource(R.drawable.about);
                if(event.getAction()==MotionEvent.ACTION_UP)
                	m_aboutBtn.setImageResource(R.drawable.about2);
                startActivity(new Intent(getApplicationContext(), About.class)); 
				//overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
                return false;
            }
			
        });
		
		m_healtipBtn.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN)
                	m_healtipBtn.setImageResource(R.drawable.tips);
                if(event.getAction()==MotionEvent.ACTION_UP)
                	m_healtipBtn.setImageResource(R.drawable.tips2);
                startActivity(new Intent(getApplicationContext(), HealthTip.class));
				//overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
                return false;
            }
			
        });
		
//		m_quitBtn.setOnTouchListener(new View.OnTouchListener() {
//
//            public boolean onTouch(View v, MotionEvent event) {
//                if(event.getAction()==MotionEvent.ACTION_DOWN)
//                	m_quitBtn.setImageResource(R.drawable.quit);
//                if(event.getAction()==MotionEvent.ACTION_UP)
//                	m_quitBtn.setImageResource(R.drawable.quit2);
//                finish();
//				//overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
//                return false;
//            }
//			
//        });
	}
	
	
//	@Override
//	protected void onResume() {
//		// TODO Auto-generated method stub
//		
//		String SDcardPath = Environment.getExternalStorageDirectory().getPath();
//		// For internal memory:
//		// "data/data/cis470.matos.sqldatabases/myfriendsDB"
//		String myDbPath = SDcardPath + "/" + "NutriDatabase";
//		IFoodHelperService service = FoodHelperServiceFactory.getInstance().getService();
//		try{
//			db = service.getDb(myDbPath);
//		}catch(Exception ex){
//			
//		}
//		
//		super.onResume();
//	}
	
	public void flagDatabase(){
		IFoodHelperService service = FoodHelperServiceFactory.getInstance().getService();
		String SDcardPath = Environment.getExternalStorageDirectory().getPath();
		String myDbPath = SDcardPath + "/" + "flagDatabaseFoodhelper";
		try{
		service.WriteData(myDbPath,"true");
		}catch(Exception ex){}
	}
	public boolean getFlag(){
		IFoodHelperService service = FoodHelperServiceFactory.getInstance().getService();
		String SDcardPath = Environment.getExternalStorageDirectory().getPath();
		String myDbPath = SDcardPath + "/" + "flagDatabaseFoodhelper";
		boolean flag = false;
		try{
			if (service.readData(myDbPath).equals("true")){
				flag = true;
			}
			}catch(Exception ex){}
		return flag;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
}
